
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>cleverhans.attacks.spatial_transformation_method &#8212; CleverHans  documentation</title>
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/alabaster.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
   
  <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for cleverhans.attacks.spatial_transformation_method</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;The SpatialTransformationMethod attack</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">warnings</span>

<span class="kn">from</span> <span class="nn">cleverhans.attacks.attack</span> <span class="kn">import</span> <span class="n">Attack</span>


<div class="viewcode-block" id="SpatialTransformationMethod"><a class="viewcode-back" href="../../../source/attacks.html#cleverhans.attacks.SpatialTransformationMethod">[docs]</a><span class="k">class</span> <span class="nc">SpatialTransformationMethod</span><span class="p">(</span><span class="n">Attack</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">  Spatial transformation attack</span>
<span class="sd">  &quot;&quot;&quot;</span>

  <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">sess</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">dtypestr</span><span class="o">=</span><span class="s1">&#39;float32&#39;</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Create a SpatialTransformationMethod instance.</span>
<span class="sd">    Note: the model parameter should be an instance of the</span>
<span class="sd">    cleverhans.model.Model abstraction provided by CleverHans.</span>

<span class="sd">  :param model: cleverhans.model.Model</span>
<span class="sd">  :param sess: optional tf.Session</span>
<span class="sd">  :param dtypestr: dtype of the data</span>
<span class="sd">  :param kwargs: passed through to super constructor</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="nb">super</span><span class="p">(</span><span class="n">SpatialTransformationMethod</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span>
        <span class="n">model</span><span class="p">,</span> <span class="n">sess</span><span class="p">,</span> <span class="n">dtypestr</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">feedable_kwargs</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;n_samples&#39;</span><span class="p">,</span> <span class="s1">&#39;dx_min&#39;</span><span class="p">,</span> <span class="s1">&#39;dx_max&#39;</span><span class="p">,</span> <span class="s1">&#39;n_dxs&#39;</span><span class="p">,</span> <span class="s1">&#39;dy_min&#39;</span><span class="p">,</span>
                            <span class="s1">&#39;dy_max&#39;</span><span class="p">,</span> <span class="s1">&#39;n_dys&#39;</span><span class="p">,</span> <span class="s1">&#39;angle_min&#39;</span><span class="p">,</span> <span class="s1">&#39;angle_max&#39;</span><span class="p">,</span>
                            <span class="s1">&#39;n_angles&#39;</span><span class="p">,</span> <span class="s1">&#39;black_border_size&#39;</span><span class="p">)</span>

<div class="viewcode-block" id="SpatialTransformationMethod.generate"><a class="viewcode-back" href="../../../source/attacks.html#cleverhans.attacks.SpatialTransformationMethod.generate">[docs]</a>  <span class="k">def</span> <span class="nf">generate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generate symbolic graph for adversarial examples and return.</span>
<span class="sd">    :param x: The model&#39;s symbolic inputs.</span>
<span class="sd">    :param kwargs: See `parse_params`</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># Parse and save attack-specific parameters</span>
    <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">parse_params</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="kn">from</span> <span class="nn">cleverhans.attacks_tf</span> <span class="kn">import</span> <span class="n">spm</span>

    <span class="n">labels</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_or_guess_labels</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">spm</span><span class="p">(</span>
        <span class="n">x</span><span class="p">,</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span>
        <span class="n">y</span><span class="o">=</span><span class="n">labels</span><span class="p">,</span>
        <span class="n">n_samples</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">n_samples</span><span class="p">,</span>
        <span class="n">dx_min</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">dx_min</span><span class="p">,</span> <span class="n">dx_max</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">dx_max</span><span class="p">,</span> <span class="n">n_dxs</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">n_dxs</span><span class="p">,</span>
        <span class="n">dy_min</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">dy_min</span><span class="p">,</span> <span class="n">dy_max</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">dy_max</span><span class="p">,</span> <span class="n">n_dys</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">n_dys</span><span class="p">,</span>
        <span class="n">angle_min</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">angle_min</span><span class="p">,</span> <span class="n">angle_max</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">angle_max</span><span class="p">,</span>
        <span class="n">n_angles</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">n_angles</span><span class="p">,</span> <span class="n">black_border_size</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">black_border_size</span><span class="p">)</span></div>

<div class="viewcode-block" id="SpatialTransformationMethod.parse_params"><a class="viewcode-back" href="../../../source/attacks.html#cleverhans.attacks.SpatialTransformationMethod.parse_params">[docs]</a>  <span class="k">def</span> <span class="nf">parse_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                   <span class="n">n_samples</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                   <span class="n">dx_min</span><span class="o">=-</span><span class="mf">0.1</span><span class="p">,</span>
                   <span class="n">dx_max</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span>
                   <span class="n">n_dxs</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span>
                   <span class="n">dy_min</span><span class="o">=-</span><span class="mf">0.1</span><span class="p">,</span>
                   <span class="n">dy_max</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span>
                   <span class="n">n_dys</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span>
                   <span class="n">angle_min</span><span class="o">=-</span><span class="mi">30</span><span class="p">,</span>
                   <span class="n">angle_max</span><span class="o">=</span><span class="mi">30</span><span class="p">,</span>
                   <span class="n">n_angles</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span>
                   <span class="n">black_border_size</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
                   <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Take in a dictionary of parameters and applies attack-specific checks</span>
<span class="sd">    before saving them as attributes.</span>
<span class="sd">    :param n_samples: (optional) The number of transformations sampled to</span>
<span class="sd">                      construct the attack. Set it to None to run</span>
<span class="sd">                      full grid attack.</span>
<span class="sd">    :param dx_min: (optional float) Minimum translation ratio along x-axis.</span>
<span class="sd">    :param dx_max: (optional float) Maximum translation ratio along x-axis.</span>
<span class="sd">    :param n_dxs: (optional int) Number of discretized translation ratios</span>
<span class="sd">                  along x-axis.</span>
<span class="sd">    :param dy_min: (optional float) Minimum translation ratio along y-axis.</span>
<span class="sd">    :param dy_max: (optional float) Maximum translation ratio along y-axis.</span>
<span class="sd">    :param n_dys: (optional int) Number of discretized translation ratios</span>
<span class="sd">                  along y-axis.</span>
<span class="sd">    :param angle_min: (optional float) Largest counter-clockwise rotation</span>
<span class="sd">                      angle.</span>
<span class="sd">    :param angle_max: (optional float) Largest clockwise rotation angle.</span>
<span class="sd">    :param n_angles: (optional int) Number of discretized angles.</span>
<span class="sd">    :param black_border_size: (optional int) size of the black border in pixels.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">n_samples</span> <span class="o">=</span> <span class="n">n_samples</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">dx_min</span> <span class="o">=</span> <span class="n">dx_min</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">dx_max</span> <span class="o">=</span> <span class="n">dx_max</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">n_dxs</span> <span class="o">=</span> <span class="n">n_dxs</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">dy_min</span> <span class="o">=</span> <span class="n">dy_min</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">dy_max</span> <span class="o">=</span> <span class="n">dy_max</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">n_dys</span> <span class="o">=</span> <span class="n">n_dys</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">angle_min</span> <span class="o">=</span> <span class="n">angle_min</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">angle_max</span> <span class="o">=</span> <span class="n">angle_max</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">n_angles</span> <span class="o">=</span> <span class="n">n_angles</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">black_border_size</span> <span class="o">=</span> <span class="n">black_border_size</span>

    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">dx_min</span> <span class="o">&lt;</span> <span class="o">-</span><span class="mi">1</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">dy_min</span> <span class="o">&lt;</span> <span class="o">-</span><span class="mi">1</span> <span class="ow">or</span> \
       <span class="bp">self</span><span class="o">.</span><span class="n">dx_max</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">dy_max</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
      <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The value of translation must be bounded &quot;</span>
                       <span class="s2">&quot;within [-1, 1]&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">kwargs</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
      <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;kwargs is unused and will be removed on or after &quot;</span>
                    <span class="s2">&quot;2019-04-26.&quot;</span><span class="p">)</span>
    <span class="k">return</span> <span class="kc">True</span></div></div>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../../../index.html">CleverHans</a></h1>








<h3>Navigation</h3>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../source/attacks.html"><cite>attacks</cite> module</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../source/model.html"><cite>model</cite> module</a></li>
</ul>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../../../index.html">Documentation overview</a><ul>
  <li><a href="../../index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" />
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>


  </body>
</html>